home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / files.swg / 0084_Get File Size in ASM.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  3.2 KB  |  95 lines

  1. {
  2.  GH> Here is what my Waite's group book says word for word, and I haven't
  3.  GH> an idea on how to do this.
  4.  GH> "23H   Filesize. Prior to invoking this function, DS:DX is set to
  5.  GH> point to the segment: offset address of an unopened file control
  6.  GH> block(FCB)."
  7.  GH> Now there is my problem, I need to know how to do the DS:DX thing to
  8.  GH> place a file name in it, so I can check that _certain_ file for its
  9.  GH> size.
  10.  GH> If anyone knows how to do this, could you lend a hand.
  11.  
  12. Refrain from using the old-style file access methods, i.e, those using
  13. FCB's. Not only are they "frowned upon" by Microsoft, worse: they can
  14. only work with files located in the current directory.
  15. Use the file handle methods instead.
  16. }
  17.  
  18.    PROGRAM The_Size_Of_Files;
  19.  
  20.    USES dos;
  21.  
  22.    { -- Both functions below wil return the size of a file. In case of an
  23.      -- error, the return value is -1.
  24.      -- Note that their parameters are of different types. }
  25.  
  26.    FUNCTION Size_of_file(CONST fn: PathStr): longint;
  27.    VAR SR: SearchRec;
  28.    BEGIN findfirst(fn, AnyFile - VolumeID - Directory, SR);
  29.          IF DosError = 0
  30.          THEN Size_of_file:=SR.Size
  31.          ELSE Size_of_file:=-1
  32.               { -- Or some other clearly nonsensical value. }
  33.    END;
  34.  
  35.    FUNCTION AsmFilesize(CONST F): longint;
  36.    { -- F MUST be a Text or File-variable. }
  37.    ASSEMBLER;
  38.    { --  LSEEK ── Move file read/write pointer (Func 42)
  39.  
  40.      -- INT 21 - DOS 2+
  41.      --    AH = 42h
  42.      --    AL = method
  43.      --       00h offset from beginning of file
  44.      --       01h offset from present location
  45.      --       02h offset from end of file
  46.      --   BX = file handle
  47.      --   CX:DX = offset in bytes
  48.  
  49.      -- Return: CF set on error
  50.      --       AX = error code (01h,06h) (see AH=59h)
  51.      --         CF clear if successful
  52.      --       DX:AX = new absolute offset from beginning of file }
  53.    ASM mov ah, $42
  54.        mov al, 2
  55.  
  56.        les di, F         { -- Now ES:DI holds the address of F. }
  57.        mov bx, es:[di]   { -- BX now holds the filehandle of F; look up
  58.                            -- types Dos.TextRec and Dos.FileRec for an
  59.                            -- explanation. }
  60.  
  61.        xor cx, cx
  62.        xor dx, dx        { -- CX and DX are now both zero. }
  63.  
  64.                          { -- In effect, the file pointer is to be moved
  65.                            -- to the end of the file. }
  66.  
  67.        int $21
  68.        jnc @@Exit        { -- Did we succeed ? }
  69.  
  70.        mov dx, $FFFF     { -- NO: so make the functionresult (a Longint is }
  71.        mov ax, $FFFF     { --     returned in DX:AX) = -1.                 }
  72.  
  73.    @@Exit:               { -- YES: quit without further ado. }
  74.    END;
  75.  
  76.    { -- Main: }
  77.  
  78.    VAR fn: PathStr;
  79.        F : FILE;
  80.  
  81.    BEGIN write('PLease enter filename: '); readln(fn);
  82.          assign(F, fn); {$I-} reset(F, 1); {$I+}
  83.          IF IOresult <> 0
  84.          THEN BEGIN writeln(#7'No such file ...'); halt(2) END;
  85.  
  86.          writeln('FileSize : ', FileSize(F));
  87.          writeln('FindFirst: ', Size_of_file(fn));
  88.          writeln('LSeek    : ', AsmFilesize(F))
  89.    END.
  90.  
  91. Perhaps it would be interesting to see which method is fastest.
  92.  
  93. The ONLY situation in which you absolutely must use FCBs is setting the
  94. volume label on a disk.
  95.